package cn.sxt.yxl.question.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.sxt.yxl.common.util.ConstatFinalUtil;
import cn.sxt.yxl.common.util.YxlSpiderUtil;
import cn.sxt.yxl.question.pojo.AColumns;
import cn.sxt.yxl.question.pojo.AItems;
import cn.sxt.yxl.question.pojo.AItemsEnum;
import cn.sxt.yxl.question.service.IAColumnsService;
import cn.sxt.yxl.question.service.IAItemsService;
import cn.sxt.yxl.question.service.ISpiderSerivce;

@Service("spiderSerivce")
public class SpiderServiceImpl implements ISpiderSerivce{

	@Resource
	IAColumnsService columnsService;
	@Resource
	IAItemsService itemsService;
	
	@Override
	public JSONObject saveItemsService(String url) {
		ConstatFinalUtil.LOGGER.info("-----ISpiderService");
		JSONObject resultObject = new JSONObject();
		Map<String,Object> condMap = new HashMap();
		
		List<AColumns> listColumns = new ArrayList<>();
		
		YxlSpiderUtil ysu = new YxlSpiderUtil();
		if(url!=null&&!"".equalsIgnoreCase(url)){
			condMap.put("url",url);
			AColumns columns = columnsService.findOne(condMap);
			listColumns.add(columns);
		}else{
			condMap.clear();
			listColumns = columnsService.findMany(condMap, null);
		}
		//遍历listColumns
		for (Iterator iterator = listColumns.iterator(); iterator.hasNext();) {
			AColumns aColumns = (AColumns) iterator.next();
			ConstatFinalUtil.LOGGER.info("正在抓取的模块:{},url:{}",aColumns.getName(),aColumns.getUrl());
			JSONObject dataJson = ysu.spider(aColumns.getUrl(),1);
			int totalPage = dataJson.getIntValue("totalPage");
			
			for (int i = 1; i <=totalPage ; i++) {
				dataJson = ysu.spider(aColumns.getUrl(),i);
				JSONArray jsonArray = dataJson.getJSONArray("data");
				for (Iterator iterator2 = jsonArray.iterator(); iterator2.hasNext();) {
					JSONObject dataJsonRes = (JSONObject) iterator2.next();
					String title = dataJsonRes.getString("title");
					String href = dataJsonRes.getString("href");
					int id = dataJsonRes.getIntValue("id");
					String imgPath = dataJsonRes.getString("imgPath");
					condMap.clear();
					condMap.put("outId", id);
					AItems aItems = itemsService.findOne(condMap);
					
					//断言
					boolean flag = false;
					if(aItems==null){
						aItems = new AItems();
						flag=true;
					}
					aItems.setContent("");
					aItems.setImgPath(imgPath);
					aItems.setName(title);
					aItems.setStatus(AItemsEnum.STATUS_DISABLE.getStatus());
					aItems.setUpdateTime(new Date());
					
					if(flag){
						aItems.setOutId(id);
						aItems.setCreateTime(new Date());
						aItems.setPubTime(new Date());
						
						itemsService.saveOne(aItems);
					}else{
						itemsService.updateOne(aItems);
					}
				}
				
			}
			
		}
		return null;
	}

}
